package com.mrent.util;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class DateJsonValueProcessor implements JsonValueProcessor {
	
	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	private DateFormat dateFormat;
	
	/**
	 * 构造方法，默认日期格式.
	 */
	public DateJsonValueProcessor(){
		this(DEFAULT_DATE_PATTERN);
	}
	
	 /** 
     * 构造方法.  
     *  
     * @param datePattern 日期格式  
     */  
    public DateJsonValueProcessor(String datePattern){   
         dateFormat = new SimpleDateFormat(datePattern); 
    }  

	public Object processArrayValue(Object arg0, JsonConfig arg1) {
		return process(arg0);
	}

	public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
		return process(arg1);
	}
	
	private Object process(Object value){
		if(value instanceof Date){
			String formatDate = dateFormat.format((Date)value);
			return formatDate;
		}else{
			return value;
		}
          
    }   

}
